Python Chapter 3

⚙️ What are Control Statements?

Control statements are used to control the flow of a program — they decide which part of the code will run and how many times.

There are 3 main types:

  1. Conditional Statements (Decision making)
  2. Looping Statements (Repetition)
  3. Jump Statements (Breaking or skipping code)

🧠 1. Conditional Statements (Decision Making)

These allow the program to make decisions based on conditions.

🔹 if Statement

Runs code only if the condition is true.

if a > b:
    print("a is greater")

🔹 if–else Statement

Runs one block if condition is true, another if false.

if a > b:
    print("a is greater")
else:
    print("b is greater")

🔹 if–elif–else Statement

Used when you have multiple conditions.

if marks >= 90:
    print("Grade A")
elif marks >= 75:
    print("Grade B")
else:
    print("Grade C")

🔁 2. Looping Statements (Repetition)

Used to repeat a block of code multiple times.

🔹 for Loop

Used when you know how many times to repeat.

for i in range(5):
    print(i)

👉 range(5) means 0 to 4.

🔹 while Loop

Used when you don’t know how many times to repeat.

i = 1
while i <= 5:
    print(i)
    i += 1

⏭️ 3. Jump Statements

Used to control the flow inside loops.

StatementUse
breakExits the loop completely
continueSkips the current iteration and goes to the next one
passDoes nothing (used as a placeholder)

Examples:

for i in range(5):
    if i == 3:
        break   # loop stops when i=3
    print(i)
for i in range(5):
    if i == 2:
        continue  # skips when i=2
    print(i)

💡 4. Nested Statements

You can use one statement inside another.

if a > 0:
    if a % 2 == 0:
        print("Positive and Even")

Leave a Comment

Your email address will not be published. Required fields are marked *